package com.work.studio.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.work.studio.R;
import com.work.studio.entitys.TodaySignEntity;
import java.util.List;

public class TodaySignAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int ITEM_HEADER = 1; // 标题
    public static final int ITEM_CONTENT = 2; // 内容

    private LayoutInflater mLayoutInflater;
    private List<TodaySignEntity> todaySignEntities = null;
    private Context mContext;
    private String TAG = this.getClass().getSimpleName();

    public TodaySignAdapter(Context context, List<TodaySignEntity> todaySignEntities) {

        this.mContext = context;
        this.todaySignEntities = todaySignEntities;
        this.mLayoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {


        switch (viewType) {
            case ITEM_HEADER:
                return new TitleHolder(mLayoutInflater.inflate(R.layout.item_type_title, parent, false));
            case ITEM_CONTENT:
                return new ContentHolder(mLayoutInflater.inflate(R.layout.item_type_content, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        int viewType = getItemViewType(position);

        switch (viewType) {
            case ITEM_HEADER:
                ((TitleHolder) holder).binHolder(todaySignEntities.get(position));
                break;
            case ITEM_CONTENT:
                ((ContentHolder) holder).binHolder(todaySignEntities.get(position));
                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        TodaySignEntity todaySignEntity  = todaySignEntities.get(position);
        int type = todaySignEntity.getType();
        if (type == 1) {
            return 1;
        } else if (type == 2) {
            return 2;
        }
        return 2;
    }

    @Override
    public int getItemCount() {
        return todaySignEntities==null?0:todaySignEntities.size();
    }

    class TitleHolder extends RecyclerView.ViewHolder {

        TextView nameValue;
        public TitleHolder(@NonNull View itemView) {
            super(itemView);
            if (itemView != null) {
                nameValue = itemView.findViewById(R.id.name_value);
            }

        }

        public void binHolder(TodaySignEntity todaySignEntity) {
            nameValue.setText(todaySignEntity.getName());
        }
    }

    class ContentHolder extends RecyclerView.ViewHolder {

        ImageView leftTop;
        ImageView leftBottom;
        ImageView rightTop;
        ImageView rightBottom;
        ImageView middle;
        TextView nameValue;
        public ContentHolder(@NonNull View itemView) {
            super(itemView);
            if (itemView != null) {
                leftTop = itemView.findViewById(R.id.left_top_value);
                leftBottom = itemView.findViewById(R.id.left_bottom_value);
                rightTop = itemView.findViewById(R.id.right_top_value);
                rightBottom = itemView.findViewById(R.id.right_bottom_value);
                middle = itemView.findViewById(R.id.middle_value);
                nameValue = itemView.findViewById(R.id.name_value);
            }

        }

        public void binHolder(TodaySignEntity todaySignEntity) {

            leftTop.setImageResource(R.drawable.left_top_blue);
            leftBottom.setImageResource(R.drawable.left_bottom_blue);
            rightTop.setImageResource(R.drawable.right_top_blue);
            rightBottom.setImageResource(R.drawable.right_bottom_blue);
            middle.setImageResource(R.drawable.middle_blue);

            if (!todaySignEntity.isHasDayWork()) {//今日是否填写日报
                middle.setImageResource(R.drawable.middle_red);
            }

            if (!todaySignEntity.isMorning_common_daka()) {//早上是否考勤打卡
                leftTop.setImageResource(R.drawable.left_top_red);
            }

            if (!todaySignEntity.isAfternoon_common_daka()) {//下午是否考勤打卡
                leftBottom.setImageResource(R.drawable.left_bottom_red);
            }

            if (!todaySignEntity.isMorning_customer_daka()) {//早上是否拜访客户打卡
                rightTop.setImageResource(R.drawable.right_top_red);
            }

            if (!todaySignEntity.isAfternoon_customer_daka()) {//下午是否拜访客户打卡
                rightBottom.setImageResource(R.drawable.right_bottom_red);
            }

            nameValue.setText(todaySignEntity.getRealName());

        }
    }
}
